package analyse;

import org.junit.Test;
import org.my.stock.util.MyUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 验证
 * */
public class Verify {
    /**
     * 验证过程：
     * 1、获取流入速度最快板块的前 5 个板块
     * 2、获取板块流入速度最快的股票 5 支股票
     * 3、每只股票各买 100 股，读取第二天收盘数据看盈亏情况
     * */
    @Test
    public void verify_stock() {
        Double all = 0D;
        Double result = 0D;
        List<List<Object>> lst = Inflow_type.inflow();
        for (int i = 1; i < 5; i++) {
            List<Object> r = lst.get(i);
            List<List<Object>> lst_stock = Inflow_stock.inflow(r.get(0).toString());
            for (int j = 0; j < 5; j++) {
                List<Object> st = lst_stock.get(j);
                Double end = getStockPrice(st.get(0).toString(), "2025-04-30");
                Double begin = getStockPrice(st.get(0).toString(), "2025-04-29");
                result += 100 * (end - begin);
                all += 100 * begin;
            }
        }
        System.out.println("总共花费：" + all + " 盈亏：" + result);
        System.out.println("涨跌："+ (result / all) * 100 +"%");
    }

    public static Double getStockPrice(String name, String date) {
        Double price = 1D;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT m.PRICE FROM df_stock_date m WHERE m.NAME = ? AND m.STOCK_DATE = ?");
            stmt.setString(1, name);
            stmt.setDate(2, MyUtil.toDate(date));
            rs = stmt.executeQuery();
            while (rs.next()) {
                price = rs.getDouble(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return price;
    }
}












































